package com.xishu.controller;

import com.xishu.annotation.valid.MethodValidator;
import com.xishu.bo.RequestBo;
import com.xishu.config.Config;
import com.xishu.response.ResponseData;
import com.xishu.response.VerifyUtil;
import com.xishu.service.CommonService;
import com.xishu.service.ObjectService;
import com.xishu.util.ImgService;
import com.xishu.util.ImgUtil;
import com.xishu.util.MongoId;
import com.xishu.util.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;

@Api(description = "图片上传")
@RestController
public class ImgController implements Tools {
    private CommonService commonService = CommonService.getInstance();

    /**
     * 先上传文档
     *
     * @param
     * @return
     */
    @ApiOperation(value = "上传图片，并规定图片的最大体积，超过了服务器会自动压缩")
    @MethodValidator(mustLogin = false)
    @PostMapping(value = ("/img/upload/{size}"), produces = ("application/json;charset=UTF-8"))
    public ResponseData uploadImgFile(@RequestParam(value = "file") MultipartFile file, @PathVariable("size") String size) throws Exception {
        File tmpFile = MongoId.getInstance().generateTmpFile("jpg");
        IOUtils.copy(file.getInputStream(), new FileOutputStream(tmpFile));

        long compressSize = getCompressSize(size);

        //图片压缩
        File compressFile = ImgUtil.compressSize(tmpFile, compressSize);

        ObjectService.getInstance().uploadObject(compressFile);

        return CommonService.getInstance().createResponse(compressFile.getName());
    }

    private long getCompressSize(String size) {
        if (size.toLowerCase().contains("k")) {
            return Long.valueOf(size.toLowerCase().split("k")[0]) * 1024;
        } else if (size.toLowerCase().contains("m")) {
            return Long.valueOf(size.toLowerCase().split("m")[0]) * 1024 * 1024;
        } else {
            return 200 * 1024;
        }
    }

    /**
     * 获取图片终端地址
     *
     * @return
     */
    @ApiOperation(value = "获取图片终端地址")
    @MethodValidator(mustLogin = false)
    @GetMapping(value = ("/img/upload"), produces = ("application/json;charset=UTF-8"))
    public ResponseData getEndPoint() throws Exception {
        Config config = Config.getInstance();
        return CommonService.getInstance().createResponse(config.getProductBucketEndpoint());
    }

    @ApiOperation(value = "生成二维码")
    @MethodValidator(mustLogin = false)
    @PutMapping(value = ("/img/qrcode"), produces = ("application/json;charset=UTF-8"))
    public ResponseData qrcode(@RequestBody RequestBo requestBo) throws Exception {
        String url = requestBo.getUrl();
        VerifyUtil.verify(() -> isNotEmpty(url));
        File file = ImgService.getInstance().createQrCode(url);
        Config config = Config.getInstance();

        ObjectService.getInstance().uploadObject(file);

        return commonService.createResponse(file.getName());
    }


}